package ar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import ar.Location.PlaceObject.PlaceCategoryManager;
import ar.Location.UI.DataSourceAdapter;
import ar.activity.R.string;

public class DataSourceActivity extends BaseActivity {

	public static Handler handlerUpdateListView = null;

	ListView lvDataSources = null;
	DataSourceAdapter dataSourceAdapter = null;
	OnItemClickListener onItemClick = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.list_data_source);
		super.onCreate(savedInstanceState);
		initEvent();
		initHandler();
		initAdapter();
		initView();
	}

	private void initEvent() {
		// TODO Auto-generated method stub
		onItemClick = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> l, View v, int position,
					long id) {
				PlaceCategoryManager.currentPlaceCategory = PlaceCategoryManager
						.dataSourceAtIndex(position);
				Intent intent = new Intent(DataSourceActivity.this,
						ListPlaceResultActivity.class);
				startActivity(intent);
			}
		};
	}

	public void initHandler() {
		// TODO Auto-generated method stub
		handlerUpdateListView = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				synchronized (dataSourceAdapter) {
					if (dataSourceAdapter != null) {
						dataSourceAdapter.notifyDataSetChanged();
					}
				}
			}
		};
	}

	private void initAdapter() {
		// TODO Auto-generated method stub
		dataSourceAdapter = new DataSourceAdapter(this);
	}

	private void initView() {
		// TODO Auto-generated method stub
		tvWhatScreen.setText(res.getString(string.dataSourceScreenName));

		lvDataSources = (ListView) findViewById(R.id.listDataSources);
		lvDataSources.setOnItemClickListener(onItemClick);
		lvDataSources.setAdapter(dataSourceAdapter);
	}
	
	@Override
	protected void onUpdateUI() {
		// TODO Auto-generated method stub
		initView();
		dataSourceAdapter.notifyDataSetChanged();
	}
}
